package com.qf.business.system.controller;

import com.qf.business.system.service.DepartmentService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.system.entity.Department;
import com.qf.data.system.vo.input.DepInput;
import com.qf.data.system.vo.output.DepListOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dep")
@Slf4j
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/list")
    public R queryDep() {
        List<Department> departments = departmentService.list();
        List<DepListOutput> outputList = departments.stream()
                .map(department -> {
                    DepListOutput output = new DepListOutput();
                    BeanUtils.copyProperties(department, output);
                    return output;
                }).collect(Collectors.toList());
        return Rs.create(outputList);
    }

    @RequestMapping("/addlist")
    public R addDep(@Validated DepInput indep) {
        Department department = new Department();
        BeanUtils.copyProperties(indep, department);
        departmentService.save(department);
        return Rs.create("新增部门成功！");
    }
}
